use actix_web::{Responder, HttpResponse, HttpRequest, Error};
use futures::future::{ready, Ready};
use chrono::NaiveDateTime;
use serde::{Deserialize, Serialize};
use rbatis::crud::CRUD;
use rbatis::rbatis::Rbatis;
use rbatis::Error as rError;

#[crud_table( id_name:id |  id_type:String |  table_name:commodity_classification |  table_columns:id,name,parent_id,remark,create_time,update_time,delete_flag )]
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct CommodityClassification{
    pub id: Option<String>,
    pub name: Option<String>,
    pub parent_id: Option<String>,
    pub remark: Option<String>,
    pub create_time: Option<NaiveDateTime>,
    pub update_time: Option<NaiveDateTime>,
    pub delete_flag: Option<i32>,
    pub subcategory_list: Option<Vec<CommodityClassification>>,
}

impl Responder for CommodityClassification {
    type Error = Error;
    type Future = Ready<Result<HttpResponse, Error>>;

    fn respond_to(self, _req: &HttpRequest) -> Self::Future {
        let body = serde_json::to_string(&self).unwrap();

        // Create response and set content type
        ready(Ok(HttpResponse::Ok()
            .content_type("application/json")
            .body(body)))
    }
}

pub async fn new_commodity_classification() -> CommodityClassification{
    CommodityClassification{
        id: None,
        name: None,
        parent_id: None,
        remark: None,
        create_time: None,
        update_time: None,
        delete_flag: None,
        subcategory_list: None
    }
}

impl CommodityClassification{
    #[sql(rbatis, "select * from commodity_classification where id = ?")]
    pub async fn find_category(&self, rbatis: &Rbatis, id :&String) -> Result<CommodityClassification, rError>{}

    #[sql(rbatis, "select * from commodity_classification where parent_id = ?")]
    pub async fn find_subcategory_list(&self, rbatis: &Rbatis, parent_id :&String) -> Result<Vec<CommodityClassification>, rError>{}

}